<?php
if (!defined('IN_IA')) {
	exit('Access Denied');
}
class Order_UnporBabyModel{

	public function updateGoodsSku($orderid = "0", $wid = '0'){
		global $_W;
		if(empty($wid)){
			$wid = $_W['warehouse']['id'];
		}
		$id = intval($orderid);
		if(empty($id)){
			return false;
		}
		$orderInfo = pdo_get("imfox_eos_order",array('uniacid'=>$_W['uniacid'],'wid'=>$wid,'id'=>$id));
		if(empty($orderInfo)){
			return "订单不存在";
		}
		$store_info = pdo_get("imfox_eos_store",array('uniacid'=>$_W['uniacid'],'id'=>$orderInfo['store_id']));
		$store_info['levelname'] = m("shop")->getLevelName($store_info['level_id']);
		$goods_list = iunserializer($orderInfo['goods_list']);
		foreach ($goods_list as &$row) {
			$row['goodssn'] = pdo_getcolumn("imfox_eos_goods",array('uniacid' => $_W['uniacid'], 'id' => $row['goodsid']),'goodssn');
			$w_goodsInfo = pdo_get("imfox_eos_warehouse_goods",array('uniacid' => $_W['uniacid'], 'id' => $row['w_goodsid']));
			$row['sku'] = $w_goodsInfo['total'];
			$row['dispatchprice'] = $w_goodsInfo['dispatchprice'];
		}
		//获取订单库存记录
		//$orderRecord = pdo_getall("imfox_eos_order_goods", array('uniacid' => $_W['uniacid'], 'wid' => $wid, 'orderid' => $id),'','w_goodsid');
		$orderRecord = pdo_getall("imfox_eos_order_goods", array('uniacid' => $_W['uniacid'], 'wid' => $wid, 'orderid' => $id));
		$recordGoodsIdArr = array();
		$goodsListIdArr = array();
		foreach ($orderRecord as $key => $value) {
			$recordGoodsIdArr[] = $value['w_goodsid'];
			
		}
		foreach ($goods_list as $value) {
			$goodsListIdArr[] = $value['w_goodsid'];
		}
		 // print_r($recordGoodsIdArr);
		 // print_r($goodsListIdArr);
		// $a = array('1','2','3','4');
		// $b = array('2','3','4','5');
		$existGoodsId = array_intersect($goodsListIdArr, $recordGoodsIdArr);
		$non_existGoodsId = array_diff($goodsListIdArr, $recordGoodsIdArr);
		$needDelGoodsId = array_diff($recordGoodsIdArr, $goodsListIdArr);
		// print_r($existGoodsId);
		// print_r($non_existGoodsId);
		// print_r($needDelGoodsId);
		//exit;
		$skulogArr = array();
		//删除
		foreach ($needDelGoodsId as $key => $goodsid) {
			$needDelGoodsArr = pdo_get("imfox_eos_order_goods", array('uniacid' => $_W['uniacid'], 'wid' => $wid, 'w_goodsid' => $goodsid, 'orderid' => $id));
			$delResult = pdo_delete("imfox_eos_order_goods", array('id' => $needDelGoodsArr['id'], 'orderid' => $id));
			if($delResult){
				//更新商品库存
				$skulogArr[] = array(
					'w_goodsid'		=>	$goodsid,
					'num'			=>	$needDelGoodsArr['total']
				);
			}
		}
		//添加
		foreach ($non_existGoodsId as $goodsid) {
			foreach ($goods_list as $value) {
				if($value['w_goodsid'] == $goodsid){
					$total = intval($value['total']);
					$insertArr = array(
						'uniacid'		=>	$_W['uniacid'],
						'wid'			=>	$wid,
						'orderid'		=>	$orderInfo['id'],
						'store_id'		=>	$orderInfo['store_id'],
						'goodsid'		=>	$value['goodsid'],
						'w_goodsid'		=>	$value['w_goodsid'],
						'price'			=>	$value['price'],
						'dispatchprice'	=>	$value['dispatchprice'],
						'total'			=>	$value['total'],
						'createtime'	=>	$orderInfo['createtime'],
						'daytime'		=>	strtotime(date("Y-m-d",$orderInfo['createtime'])),
						'openid'		=>	$orderInfo['openid'],
						'title'			=>	$value['goodsname'],
						'remark'		=>	$value['remark']
					);
					pdo_insert("imfox_eos_order_goods",$insertArr);
					//更新商品库存
					$skulogArr[] = array(
						'w_goodsid'		=>	$value['w_goodsid'],
						'num'			=>	-$total
					);
				}
			}
		}
		//更新
		$orderRecord = array();
		$orderRecord = pdo_getall("imfox_eos_order_goods", array('uniacid' => $_W['uniacid'], 'wid' => $wid, 'orderid' => $id),'','w_goodsid');
		foreach ($existGoodsId as $goodsid) {
			foreach ($goods_list as $value) {
				if($value['w_goodsid'] == $goodsid){
					if($value['total'] != $orderRecord[$value['w_goodsid']]['total']){
						//数量不同，更新库存
						$skulogArr[] = array(
							'w_goodsid'		=>	$value['w_goodsid'],
							'num'			=>	$orderRecord[$value['w_goodsid']]['total'] - $value['total']
						);
					}
					//更新数据
					pdo_update("imfox_eos_order_goods", array('total' => $value['total'], 'price' => $value['price'], 'remark' => $value['remark'], 'dispatchprice' => $value['dispatchprice']), array('w_goodsid' => $value['w_goodsid'], 'orderid' => $id));
					
				}
			}
		}
		//更新商品库存
		foreach ($skulogArr as $value) {
			pdo_update("imfox_eos_warehouse_goods",array("total +=" => $value['num'], 'salesreal -=' => $value['num']), array('uniacid' => $_W['uniacid'], 'id' => $value['w_goodsid']));
			com('log')->skuLog('2', $value['w_goodsid'], $value['num'], $remark = "操作订单更新库存", array('order_id' => $orderInfo['id']));
		}
	}
	public function splitOrder($orderid = "0", $wid = '0', $paylogId = '0'){
		global $_W;
		if(empty($wid)){
			$wid = $_W['warehouse']['id'];
		}
		$id = intval($orderid);
		if(empty($id)){
			return false;
		}
		$orderInfo = pdo_get("imfox_eos_order",array('uniacid'=>$_W['uniacid'],'wid'=>$wid,'id'=>$id));
		if(empty($orderInfo)){
			return false;
		}
		$goods_list = iunserializer($orderInfo['goods_list']);
		$splitGoodsArr = array();
		$is_sub_wgoods = 0;
		foreach ($goods_list as $key => &$row) {
			if(!empty($row['is_sub_wid'])){
				$splitGoodsArr[$row['is_sub_wid']][] = $row;
				$is_sub_wgoods = 1;
			}else{
				$splitGoodsArr[$wid][] = $row;
			}
		}
		if($is_sub_wgoods){
			//更新原订单
			pdo_update("imfox_eos_order", array('is_blanket_order' => '1'), array('id' => $id));
			foreach ($splitGoodsArr as $key => $value) {
				$insertArr = array(
					'uniacid'		=>	$_W['uniacid'],
					'wid'			=>	$key,
					'store_id'		=>	$orderInfo['store_id'],
					'ordersn'		=>	$orderInfo['ordersn'] . "-" . $key,
					'openid'		=>	$orderInfo['openid'],
					'pay_type'		=>	$orderInfo['pay_type'],


					// 'total_price'	=>	$totalPrice,
					// 'pay_price'		=>	$payPrice,
					// 'coupon_id'		=>	$couponId,
					'coupon_price'	=>	0,
					'enough_deduct'	=>	0,
					'delivery_time'	=>	$orderInfo['delivery_time'],
					'pay_status'	=>	$orderInfo['pay_status'],
					'order_status'	=>	$orderInfo['order_status'],
					'remark'		=>	$orderInfo['remark'],
					'createtime'	=>	$orderInfo['createtime'],
					'paytime'		=>	$orderInfo['paytime'],
					'goods_list'	=>	iserializer($value),
					'goods_list_old'=>	iserializer($value),
				);
				if($key == $wid && $orderInfo['enough_deduct'] > 0){
					$insertArr['enough_deduct'] = $orderInfo['enough_deduct'];
				}
				$totalPrice = 0;
				foreach ($value as $key2 => $value2) {
					$totalPrice += $value2['price']*$value2['total'];
				}
				$insertArr['total_price'] = $totalPrice;
				$couponId = intval($orderInfo['coupon_id']);
				if(!empty($couponId)){
					$couponInfo = pdo_get('imfox_eos_coupon', array('id' => $couponId));
					if(empty($couponInfo['is_sub_wid'])){
						//该优惠券为主仓优惠券
						if($couponInfo['wid'] == $key){
							$insertArr['coupon_id'] = $couponId;
							$insertArr['coupon_price'] = $orderInfo['coupon_price'];
						}
					}else{
						//该优惠券为副仓优惠券
						if($couponInfo['is_sub_wid'] == $key){
							$insertArr['coupon_id'] = $couponId;
							$insertArr['coupon_price'] = $orderInfo['coupon_price'];
						}
					}
				}
				$insertArr['pay_price'] = number_format($totalPrice - $insertArr['enough_deduct'] - $insertArr['coupon_price'],'2','.','');
				
				
				$insertRes = pdo_insert("imfox_eos_order",$insertArr);
				$orderId = pdo_insertid();
				if($orderId && $couponId > 0){
					pdo_update("imfox_eos_coupon_record", array('status' => '1', 'usedtime' => TIMESTAMP, 'order_id' => $orderId), array('id' => $couponId));
				}
				if($orderInfo['pay_type'] == '1' && $orderId){

					$logArr = array(
						'uniacid'			=>	$_W['uniacid'],
						'wid'				=>	$key,
						'orderid'			=>	$orderId,
						'pay_type'			=>	'1',
						'pay_price'			=>	$insertArr['pay_price'],
						'fromid'			=>	$orderInfo['openid'],
						'createtime'		=>	TIMESTAMP,
						'is_split_order'	=>	'1',
						'from_paylog_id'	=>	$paylogId
					);
					pdo_insert('imfox_eos_order_paylog', $logArr);
				}
			}
			return true;
		}else{
			return false;
		}
	}
}

?>